package com.xzx.zookeeper.lock;

import java.sql.Timestamp;

/**
 * @version 1.0
 * @auther xinzhixuan
 * @date 2022/9/3 13:18
 */
public class TestDistributeLock {
    public static void main(String[] args) throws InterruptedException {
        DistributeLock distributeLock = new  DistributeLock(ZkClient.getInstance());
        DistributeLock distributeLock2 = new  DistributeLock(ZkClient.getInstance());

        new Thread(()->{
            try {
                System.out.println(new Timestamp(System.currentTimeMillis()) + " 线程1开始获取锁");
                distributeLock.lock();
                System.out.println(new Timestamp(System.currentTimeMillis()) + " 线程1成功获取锁");

                System.out.println(new Timestamp(System.currentTimeMillis()) + " 线程1 开始执行任务，耗时5秒");
                Thread.sleep(5000);

                distributeLock.unlock();
                System.out.println(new Timestamp(System.currentTimeMillis()) + " 线程1释放锁");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }).start();

        new Thread(()->{
            try {
                System.out.println(new Timestamp(System.currentTimeMillis()) + " 线程2开始获取锁");
                distributeLock2.lock();
                System.out.println(new Timestamp(System.currentTimeMillis()) + " 线程2成功获取锁");

                System.out.println(new Timestamp(System.currentTimeMillis()) + " 线程2 开始执行任务，耗时15秒");
                Thread.sleep(15000);

                distributeLock2.unlock();
                System.out.println(new Timestamp(System.currentTimeMillis()) + " 线程2释放锁");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();


    }
}
